package com.kong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kong.constant.MessageConstant;
import com.kong.entity.PageResult;
import com.kong.entity.QueryPageBean;
import com.kong.entity.Result;
import com.kong.pojo.Menu;
import com.kong.service.MenuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yang2013
 * @data 2020/10/19 13:09
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menus=menuService.findAll();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menus);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @PostMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=menuService.findByPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu){
        try {
            menuService.addMenu(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (RuntimeException r){
            return new Result(false, r.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    @GetMapping("/deleteById")
    public Result delete(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //修改时，前端设置菜单名称不允许修改不允许进行修改
    @PostMapping("/edit")
    public Result update(@RequestBody Menu menu){
        try{
            menuService.updateMenu(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }


    //将菜单进行缓存--使用用户名+固定字符串作为key的值
    @GetMapping("/dynamic")
    public Result dynamicMenu(String username){
        try{
            List<Menu> menus=menuService.dynamicMenu(username);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menus);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

}
